﻿// -----------------------------------------------------------------------
// <copyright file="RequirementsListData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.VONE.ENTITIES;
    
    /// <summary>
    /// Class access to db RequirementsList
    /// </summary>
    public class RequirementsListData
    {
        /// <summary>
        /// Get RequirementsList By Id
        /// </summary>
        /// <param name="requirementsListId">Param requirementsListId</param>
        /// <returns>Return RequirementsList</returns>
        public RequirementsListEN GetRequirementsListById(int requirementsListId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                RequirementsListEN requirementsList = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneRequirementsListGetById, requirementsListId);
                while (reader.Read())
                {
                    requirementsList = new RequirementsListEN();

                    if (!reader.IsDBNull(0))
                    {
                        requirementsList.RequirementsListId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        requirementsList.Name = reader.GetString(1);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return requirementsList;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all RequirementsList
        /// </summary>
        /// <returns>Return list all RequirementsList</returns>
        public IList<RequirementsListEN> GetRequirementsLists()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<RequirementsListEN> lstRequirementsList = new List<RequirementsListEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneRequirementsListGetAll);
                while (reader.Read())
                {
                    RequirementsListEN requirementsList = new RequirementsListEN();

                    if (!reader.IsDBNull(0))
                    {
                        requirementsList.RequirementsListId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        requirementsList.Name = reader.GetString(1);
                    }

                    lstRequirementsList.Add(requirementsList);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstRequirementsList;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Insert to db RequirementsList
        /// </summary>
        /// <param name="requirementsList">Paarm RequirementsList</param>
        /// <returns>Insert RequirementsList</returns>
        public int Insert(RequirementsListEN requirementsList)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.RequirementsList.Name, requirementsList.Name)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneRequirementsListInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Update to db RequirementsList
        /// </summary>
        /// <param name="requirementsList">Param RequirementsList</param>
        public void Update(RequirementsListEN requirementsList)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.RequirementsList.RequirementsListId, requirementsList.RequirementsListId),
                                    new SqlParameter(Constants.SqlParamerterName.RequirementsList.Name, requirementsList.Name),
                                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneRequirementsListUpdate, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete RequirementsList by requirementsListId
        /// </summary>
        /// <param name="requirementsListId">param requirementsListId</param>
        public void Delete(int requirementsListId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.RequirementsList.RequirementsListId, requirementsListId)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneRequirementsListDelete, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Exist name
        /// </summary>
        /// <param name="requirementsListId">Param requirementsListId</param>
        /// <param name="name">Param name</param>
        /// <returns>Return true or false</returns>
        public bool Exist(int requirementsListId, string name)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.RequirementsList.RequirementsListId, requirementsListId),
                    new SqlParameter(Constants.SqlParamerterName.RequirementsList.Name, name)
                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneRequirementsListExist, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result > 0;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all RequirementsList By Name
        /// </summary>
        /// <param name="profileTypeId">param profileTypeId</param>
        /// <param name="name">param name</param>
        /// <returns>Return list all RequirementsList</returns>
        public IList<RequirementsListEN> GetRequirementsListSearch(int profileTypeId, string name)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<RequirementsListEN> lstRequirementsList = new List<RequirementsListEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneRequirementsListSearch, profileTypeId, !string.IsNullOrWhiteSpace(name) ? name.Trim() : (object)DBNull.Value);
                while (reader.Read())
                {
                    RequirementsListEN requirementsList = new RequirementsListEN();

                    if (!reader.IsDBNull(0))
                    {
                        requirementsList.RequirementsListId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        requirementsList.Name = reader.GetString(1);
                    }

                    lstRequirementsList.Add(requirementsList);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstRequirementsList;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get RequirementsList By Name
        /// </summary>
        /// <param name="name">Param name</param>
        /// <returns>Return RequirementsList</returns>
        public RequirementsListEN GetRequirementsListByName(string name)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                RequirementsListEN requirementsList = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneRequirementsListGetByName, name);
                while (reader.Read())
                {
                    requirementsList = new RequirementsListEN();

                    if (!reader.IsDBNull(0))
                    {
                        requirementsList.RequirementsListId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        requirementsList.Name = reader.GetString(1);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return requirementsList;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all LegalGroundsList By profileTypeId
        /// </summary>
        /// <param name="profileTypeId">param profileTypeId</param>
        /// <param name="name">param name</param>
        /// <returns>Return list all LegalGroundsList</returns>
        public IList<RequirementsListEN> GetRequirementsListByProfileTypeId(int profileTypeId, string name)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<RequirementsListEN> lstRequirementsList = new List<RequirementsListEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneRequirementsListGetByProfileTypeId, profileTypeId, !string.IsNullOrWhiteSpace(name) ? name.Trim() : (object)DBNull.Value);
                while (reader.Read())
                {
                    RequirementsListEN requirementsListEn = new RequirementsListEN();

                    if (!reader.IsDBNull(0))
                    {
                        requirementsListEn.RequirementsListId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        requirementsListEn.Name = reader.GetString(1);
                    }

                    lstRequirementsList.Add(requirementsListEn);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstRequirementsList;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}